package com.gxa.controller;

import com.gxa.common.PaginationResult;
import com.gxa.common.ResponseData;
import com.gxa.pojo.*;
import com.gxa.service.StoreService;
import com.gxa.utils.UploadFileUtil;
import io.swagger.annotations.*;
import org.apache.commons.fileupload.FileUploadException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: zrf.
 * Date: 2020/10/26 21:49
 * Description:门店管理
 * Version: V1.0
 */
@RestController
@RequestMapping("/store")
@Api(value = "门店管理",tags = "门店管理接口API")
public class StoreController {
    @Autowired
    StoreService storeService;
    @Autowired
    private ResponseData responseData;
//    @RequestMapping(value = "/selCupro",method = RequestMethod.GET)
//    @ApiOperation(value = "查询总部信息",notes = "查询总部信息",httpMethod = "get", response = ResponseData.class)
//    @ResponseBody
//    public ResponseData selCupro(@RequestParam(defaultValue = "1") Integer currentPage, @RequestParam(defaultValue = "5") Integer pageSize){
//        System.out.println("=======");
//
//        PaginationResult paginationResult = storeService.selCupro(currentPage,pageSize);
//        if(paginationResult.getData() != null){
//            responseData.setCode(200);
//            responseData.setMsg("成功");
//            responseData.setData(paginationResult);
//        }else{
//            responseData.setCode(400);
//            responseData.setMsg("失败");
//        }
//
//
//        return responseData;
//    }
//
//
//    @RequestMapping(value = "/selHeadq",method = RequestMethod.GET)
//    @ApiOperation(value = "搜索总部",notes = "根据总部名称搜索总部",httpMethod = "get", response = ResponseData.class)
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "headquarters_name",value = "总部名称",paramType = "query"),
//    })
//    @ResponseBody
//    public ResponseData selHeadq(@RequestParam(defaultValue = "5") String headquarters_name){
//        System.out.println(headquarters_name);
//        Headquarters headquarters = storeService.selHeadqa(headquarters_name);
//        if(headquarters != null){
//            responseData.setCode(200);
//            responseData.setMsg("成功");
//            responseData.setData(headquarters);
//        }else{
//            responseData.setCode(400);
//            responseData.setMsg("未找到数据");
//        }
//        return responseData;
//    }
//
//
//    @RequestMapping(value = "/updateheadq",method = RequestMethod.POST)
//    @ApiOperation(value = "编辑总部信息",notes = "编辑总部信息",httpMethod = "post", response = ResponseData.class)
//    @ResponseBody
//    public ResponseData updateHeadq(@RequestBody @ApiParam(name = "headquarters", value = "总部信息",required = true) Headquarters headquarters,HttpServletRequest request) throws IOException, FileUploadException {
//        System.out.println(headquarters.toString());
//        String[] string= UploadFileUtil.uploadFile(request, headquarters.getFile());
//        headquarters.setDetailsHeadquartersStores(string[0]);
//        int result = storeService.updateHeadq(headquarters);
//        if(result > 0){
//            responseData.setCode(200);
//            responseData.setMsg("成功");
//        }else{
//            responseData.setCode(400);
//            responseData.setMsg("失败");
//        }
//        return responseData;
//    }
//
//    @RequestMapping(value = "/addheadq",method = RequestMethod.POST)
//    @ApiOperation(value = "增加总部信息",notes = "增加总部信息",httpMethod = "post", response = ResponseData.class)
//    @ResponseBody
//    public ResponseData addHeadq(@RequestBody @ApiParam(name = "headquarters", value = "总部信息",required = true) Headquarters headquarters, HttpServletRequest request) throws IOException, FileUploadException {
//        System.out.println(headquarters.toString());
//        String[] string=UploadFileUtil.uploadFile(request,headquarters.getFile());
//        headquarters.setDetailsHeadquartersStores(string[0]);
//        int result = storeService.addHeadq(headquarters);
//        ResponseData responseData = new ResponseData();
//        if(result > 0){
//            responseData.setCode(200);
//            responseData.setMsg("成功");
//        }else{
//            responseData.setCode(400);
//            responseData.setMsg("失败");
//        }
//        return responseData;
//    }
//
//
//    @RequestMapping(value = "/delheadq",method = RequestMethod.POST)
//    @ApiOperation(value = "删除总部",notes = "删除总部",httpMethod = "post", response = ResponseData.class)
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "headquartersCode",value = "总部编码",paramType = "query")
//    })
//    @ResponseBody
//    public ResponseData delHeadq(@RequestParam Integer headquartersCode){
//        System.out.println(headquartersCode);
//        int result = storeService.delHeadq(headquartersCode);
//        if(result > 0){
//            responseData.setCode(200);
//            responseData.setMsg("成功");
//        }else{
//            responseData.setCode(400);
//            responseData.setMsg("失败");
//        }
//        return responseData;
//    }

    @RequestMapping(value = "/selbreach",method = RequestMethod.GET)
    @ApiOperation(value = "搜索分店",notes = "根据总部id搜索该总部对应的所有分店",httpMethod = "get", response = ResponseData.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "currentPage",value = "页数",paramType = "query"),
            @ApiImplicitParam(name = "pageSize",value = "总条数",paramType = "query"),
    })
    @ResponseBody
    public ResponseData selBreach(@RequestParam(defaultValue = "1") Integer currentPage, @RequestParam(defaultValue = "5") Integer pageSize){
       PaginationResult paginationResult = storeService.selBreach(currentPage,pageSize);
       if(paginationResult.getData() != null){
           responseData.setCode(200);
           responseData.setMsg("成功");
           responseData.setData(null);
       }else{
           responseData.setCode(400);
           responseData.setMsg("失败");
           responseData.setData(null);
       }
        return responseData;
    }

    @RequestMapping(value = "/selbreachmag",method = RequestMethod.GET)
    @ApiOperation(value = "搜索分店信息",notes = "根据分店编码搜索该分店具体信息",httpMethod = "get", response = ResponseData.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "branchCode",value = "分店编码",paramType = "query"),
    })
    @ResponseBody
    public ResponseData selBreachMag(@RequestParam Integer branchCode){
        System.out.println(branchCode);
        Branch branch = storeService.selBreaChMag(branchCode);
        if(branch != null){
            responseData.setData(branch);
            responseData.setCode(200);
            responseData.setMsg("成功");
        }else{
            responseData.setCode(400);
            responseData.setMsg("未找到数据");
            responseData.setData(null);
        }
        return responseData;
    }

//    @RequestMapping(value = "/selclzzb",method = RequestMethod.GET)
//    @ApiOperation(value = "搜索类库",notes = "根据总部id搜索该总部对应的所有类库",httpMethod = "get", response = ResponseData.class)
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "headquartersCode",value = "总部id",paramType = "query"),
//    })
//    @ResponseBody
//    public ResponseData selClzzb(@RequestParam Integer headquartersCode,@RequestParam(defaultValue = "1") Integer currentPage, @RequestParam(defaultValue = "5") Integer pageSize){
//        System.out.println(headquartersCode);
//        PaginationResult paginationResult = storeService.selClazzb(headquartersCode,currentPage,pageSize);
//        if(paginationResult.getData() != null){
//            responseData.setCode(200);
//            responseData.setMsg("成功");
//        }else{
//            responseData.setCode(400);
//            responseData.setMsg("失败");
//        }
//        return responseData;
//    }


    @RequestMapping(value = "/addbranch",method = RequestMethod.POST)
    @ApiOperation(value = "增加分店信息",notes = "增加分店信息",httpMethod = "post", response = ResponseData.class)
    @ResponseBody
    public ResponseData addBranch(@RequestBody @ApiParam(name = "branch", value = "分店",required = true) Branch branch) throws IOException, FileUploadException {
        System.out.println(branch.toString());
        int result = storeService.addBranch(branch);
        if(result > 0){
            responseData.setCode(200);
            responseData.setMsg("成功");
            responseData.setData(null);
        }else{
            responseData.setCode(400);
            responseData.setMsg("失败");
            responseData.setData(null);
        }
        return responseData;
    }

//    @RequestMapping(value = "/addclazz",method = RequestMethod.POST)
//    @ApiOperation(value = "增加类库",notes = "增加类库",httpMethod = "post", response = ResponseData.class)
//    @ResponseBody
//    public ResponseData addClazz(@RequestBody @ApiParam(name = "clazzLibrary", value = "类库",required = true) ClazzLibrary clazzLibrary){
//        System.out.println(clazzLibrary.toString());
//        int result = storeService.addClazz(clazzLibrary);
//        if(result > 0){
//            responseData.setCode(200);
//            responseData.setMsg("成功");
//        }else{
//            responseData.setCode(400);
//            responseData.setMsg("失败");
//        }
//        return responseData;
//    }

//    @RequestMapping(value = "/updateclazz",method = RequestMethod.POST)
//    @ApiOperation(value = "编辑类库",notes = "编辑类库",httpMethod = "post", response = ResponseData.class)
//    @ResponseBody
//    public ResponseData updateClazz(ClazzLibrary clazzLibrary){
//        System.out.println(clazzLibrary.toString());
//        int result = storeService.updateClazz(clazzLibrary);
//        if(result > 0){
//            responseData.setCode(200);
//            responseData.setMsg("成功");
//        }else{
//            responseData.setCode(400);
//            responseData.setMsg("失败");
//        }
//        return responseData;
//    }

//    @RequestMapping(value = "/delclazz",method = RequestMethod.POST)
//    @ApiOperation(value = "删除类库",notes = "删除类库",httpMethod = "post", response = ResponseData.class)
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "clazzLibraryId",value = "类库id",paramType = "query")
//    })
//    @ResponseBody
//    public ResponseData delClazz(@RequestParam Integer clazzLibraryId){
//        System.out.println(clazzLibraryId);
//        int result = storeService.delClazz(clazzLibraryId);
//        if(result > 0){
//            responseData.setCode(200);
//            responseData.setMsg("成功");
//        }else{
//            responseData.setCode(400);
//            responseData.setMsg("失败");
//        }
//        return responseData;
//    }

    @RequestMapping(value = "/updetebranch",method = RequestMethod.POST)
    @ApiOperation(value = "编辑分店信息",notes = "编辑分店信息",httpMethod = "post", response = ResponseData.class)
    @ResponseBody
    public ResponseData updateBranch(@RequestBody @ApiParam(name = "branch", value = "分店",required = true) Branch branch) throws IOException, FileUploadException {
        System.out.println(branch.toString());
        int result = storeService.updateBranch(branch);
        if(result > 0){
            responseData.setCode(200);
            responseData.setMsg("成功");
            responseData.setData(null);
        }else{
            responseData.setCode(400);
            responseData.setMsg("失败");
            responseData.setData(null);
        }
        return responseData;
    }


    @RequestMapping(value = "/delbranch",method = RequestMethod.POST)
    @ApiOperation(value = "删除分店信息",notes = "删除分店信息",httpMethod = "post", response = ResponseData.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "branchCode",value = "分店编码",paramType = "query")
    })
    @ResponseBody
    public ResponseData editBranch(@RequestParam Integer branchCode){
        System.out.println(branchCode);
        int result = storeService.delBranch(branchCode);
        if(result > 0){
            responseData.setCode(200);
            responseData.setMsg("成功");
            responseData.setData(null);
        }else{
            responseData.setCode(400);
            responseData.setMsg("失败");
            responseData.setData(null);
        }
        return responseData;
    }


    @RequestMapping(value = "/queryBreachAll",method = RequestMethod.GET)
    @ApiOperation(value = "查询所有分店信息",notes = "查询所有分店信息",httpMethod = "get", response = ResponseData.class)
    @ResponseBody
    public ResponseData queryBreachAll(){
        List<Branch> list = storeService.queryBranch();
        if(list != null){
            responseData.setData(list);
            responseData.setCode(200);
            responseData.setMsg("成功");
        }else{
            responseData.setCode(400);
            responseData.setMsg("失败");
            responseData.setData(null);
        }
        return responseData;
    }

//    //动态下拉框
//    @RequestMapping(value = "/queryHeadqaCode",method = RequestMethod.GET)
//    public ResponseData selHeadqaCode(){
//        List<Integer> integerList = storeService.selHeadqaCode();
//        if(integerList != null){
//            responseData.setData(integerList);
//            responseData.setCode(200);
//            responseData.setMsg("成功");
//        }else{
//            responseData.setCode(400);
//            responseData.setMsg("失败");
//        }
//        return responseData;
//    }

}
